package com.nineclock.system.controller;

import com.nineclock.common.entity.Result;
import com.nineclock.system.dto.SysAllowedJoinCompanyUserDTO;
import com.nineclock.system.dto.SysApplyJoinCompanyUserDTO;
import com.nineclock.system.dto.SysCompanyDTO;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.service.SysCompanyService;
import com.nineclock.system.service.SysCompanyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("company")
@Api(value = "企业管理接口", tags = "系统服务")
public class SysCompanyController {

    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private SysCompanyUserService companyUserService;

    /**
     * 企业管理: 根据系统用户ID查询企业列表
     * 接口路径：GET/sys/company/list/{userId}
     */
    @ApiOperation("企业管理: 根据系统用户ID查询企业列表")
    @GetMapping("list/{userId}")
    public Result<List<SysCompanyDTO>> queryUserJoinCompany(@PathVariable Long userId){
        List<SysCompanyDTO> companyDTOList = companyService.queryUserJoinCompany(userId);
        return Result.success(companyDTOList);
    }

    /**
     * 企业管理: 查询当前企业信息
     * 接口路径：GET/sys/company
     */
    @GetMapping
    @ApiOperation("企业管理: 查询当前企业信息")
    public Result<SysCompanyDTO> queryCompanyInfo(){
        SysCompanyDTO companyDTO = companyService.queryCompanyInfo();
        return Result.success(companyDTO);
    }

    /**
     * 企业管理: 上传图片
     * 接口路径：POST/sys/company/uploadOSS
     */
    @PostMapping("uploadOSS")
    @ApiOperation("企业管理: 上传图片")
    public Result<String> uploadOSS(MultipartFile file) throws Exception{
        String url = companyService.uploadOSS(file);
        return Result.success(url);
    }

    /**
     * 企业管理: 更新当前企业的基本信息
     * 接口路径：PUT/sys/company
     */
    @PutMapping
    @ApiOperation("企业管理: 更新当前企业的基本信息")
    public Result updateCompanyInfo(@RequestBody SysCompanyDTO companyDTO){
        companyService.updateCompanyInfo(companyDTO);
        return Result.success();
    }

    /**
     * 企业管理: 获取企业当前主管理员
     * 接口路径：GET/sys/company/getCurrentAdmin
     */
    @GetMapping("getCurrentAdmin")
    @ApiOperation("企业管理: 获取企业当前主管理员")
    public Result<SysCompanyUserDTO> getCurrentAdmin(){
        SysCompanyUserDTO companyUserDTO = companyUserService.getCurrentAdmin();
        return Result.success(companyUserDTO);
    }

    /**
     * 企业管理: 更改系统管理员
     * 接口路径：POST/sys/company/config/admin
     */
    @PostMapping("config/admin")
    @ApiOperation("企业管理: 更改系统管理员")
    public Result changeSysAdmin(@RequestBody Map map){
        // 获取员工id、验证码
        String code = (String) map.get("code");
        Long userId = Long.valueOf(map.get("userId").toString());
        companyService.changeSysAdmin(code, userId);
        return Result.success();
    }

    /**
     * 根据关键字名称查询企业
     * 接口路径：GET/sys/company/list
     */
    @GetMapping("list")
    @ApiOperation("根据关键字名称查询企业")
    public Result<List<SysCompanyDTO>> queryCompanyByName(String keyword){
        List<SysCompanyDTO> companyDTOList = companyService.queryCompanyByName(keyword);
        return Result.success(companyDTOList);
    }

    /**
     * 加入企业申请
     * 接口路径：POST/sys/company/applyJoinCompany
     */
    @PostMapping("applyJoinCompany")
    @ApiOperation("加入企业申请")
    public Result applyJoinCompany(@RequestBody SysApplyJoinCompanyUserDTO applyJoinCompanyUserDTO){
        companyService.applyJoinCompany(applyJoinCompanyUserDTO);
        return Result.success();
    }

    /**
     * 加入企业审核
     * 接口路径：POST/sys/company/allowedJoinCompany
     */
    @PostMapping("allowedJoinCompany")
    @ApiOperation("加入企业审核")
    public Result allowedJoinCompany(SysAllowedJoinCompanyUserDTO allowedJoinCompanyUserDTO){
        companyService.allowedJoinCompany(allowedJoinCompanyUserDTO);
        return Result.success();
    }

}
